package com.study.service.impl;

import com.study.dao.AdminInfoDAO;
import com.study.dao.impl.AdminInfoDAOImpl;
import com.study.dto.AuthDto;
import com.study.pojo.AdminInfo;
import com.study.service.AdminService;
import com.study.util.MD5;

/**
 * @PackgeName: com.study.service.impl
 * @ClassName: AdminServiceImpl
 * @Author: 严志祥
 * Date: 2022-05-11 18:18
 * project name: web_project_medal
 * @Version:
 * @Description:
 */
public class AdminServiceImpl implements AdminService {
    private AdminInfoDAO dao = new AdminInfoDAOImpl();

    @Override
    public AuthDto login(String username, String password) throws Exception {
        //1.验证参数
        if(username==null || password==null || "".equals(username) || "".equals(password)){
            throw new Exception("用户名和密码不能为空");
        }
        //2.根据用户获取用户参数
        AdminInfo adminInfo = null;
        try{
            adminInfo = dao.selectByName(username);
        }catch (Exception e){
            throw new Exception("用户不存在");
        }
        if(!dao.selectByAdminName(username)){
            throw new Exception("用户不存在");
        }
        //3.验证密码的正确性
        if(!MD5.encode(password).equals(adminInfo.getAdminPassword())){
            throw new Exception("密码错误");
        }
        AuthDto auth = new AuthDto();
        auth.setAdminId(adminInfo.getAdminId());
        auth.setAdminName(adminInfo.getAdminName());
        return auth;
    }
}
